package com.wfm.leetcode.editor.cn;

import java.util.Arrays;

public class P面试题最小差{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int smallestDifference(int[] a, int[] b) {
        Arrays.sort(a);
        Arrays.sort(b);

        long minDiff = Long.MAX_VALUE;
        int i = 0, j = 0;

        while (i < a.length && j < b.length) {
            long diff = Math.abs((long)a[i] - (long)b[j]);
            minDiff = Math.min(minDiff, diff);

            if (a[i] < b[j]) {
                i++;
            } else {
                j++;
            }
        }

        return (int)(minDiff);
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new P面试题最小差().new Solution();
        int[] a = new int[]{-2147483648,1};
        int[] b = new int[]{2147483647,0};
        int i = solution.smallestDifference(a, b);
        System.out.println(i);
    }
}